// ============================================================================
// PAUL SCHOBERG
// CS294, Spring 2000
// Dr. Wang
// ----------------------------------------------------------------------------
// Homeowrk 6 - JDBC
// ============================================================================

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import Rand.*;
import ScreenIO.*;
import ExtensionFilter.*;

public	class		Homework6

	extends		JFrame
	
	implements 	ActionListener,
			WindowListener,
			FocusListener,
			MouseListener,
			SwingConstants
{

// --- Homework6:Data Members -------------------------------------------------

	public int		scrWidth,scrHeight,winWidth,winHeight;
				
	public Container	mainWindow;
	public GridBagConstraints g;
	
	public JButton		btn[];
	public final int	quitButton	=0,
				searchButton	=1,
				addButton	=2,
				deleteButton	=3,
				saveButton	=4;
	public final String	btnText[]={""       ,"Search","Add","Del","Save Data"};
	public final String	btnIcon[]={"Top.gif",""      ,""   ,""   ,""         };
	public final String	btnToolTip[]={
					"Quit the program",
					"Search for last names or first names beginning with...",
					"Add a new entry to the database",
					"Delete this entry from the database",
					"Save the changes"
				};

	public JLabel		display,main,statusMsg;
	
	public JLabel		companyText	,streetText	,officePhoneText,		 //filterText	,
				lastNameText	,firstNameText	,cityText	,homePhoneText	,
				memoText	,stateText	,zipText	,pagerText	,
						 eMailText	,		 faxText;
	public JTextField	companyName	,street		,officePhone	,
				lastName	,firstName	,city		,homePhone	,filter		,
						 state		,zip		,pager		,
						 eMail		,		 fax;
	public JTextArea	memo;
	
	public JTable		table;
	public HW6TableModel	tableModel;
	
	public int		recordStatus;
	public final int	NO_RECORD=0,INSERT_RECORD=1,UPDATE_RECORD=2;
	
	public boolean		goodToGo;
	
	public JMenuBar		menu		=null;
	
	public JMenu		fileMenu	=new JMenu("File");
	public JMenuItem	fileOpen	=new JMenuItem("Open");
	public JMenuItem	fileExit	=new JMenuItem("Exit");
	
	public JMenu		saveMenu	=new JMenu("Save");
	public JMenuItem	saveFields 	=new JMenuItem("Fields");
	public JMenuItem	saveLabels 	=new JMenuItem("Labels");
	
	public JMenu		helpMenu	=new JMenu("Help ");
	public JMenuItem	helpAbout	=new JMenuItem("About");
	
	public String		origLName,origFName;
	
// --- Homework6:CONSTRUCTOR --------------------------------------------------

	public Homework6() {
		super("CS294 - JDBC - Paul Schoberg");
		
		recordStatus	=NO_RECORD;
		goodToGo	=false;

		mainWindow	=getContentPane();

		menu		=new JMenuBar();

		fileMenu.add(fileOpen);			fileOpen.setMnemonic('p');		fileOpen.addActionListener(this);
		fileMenu.add(fileExit);			fileExit.setMnemonic('x');		fileExit.addActionListener(this);
		menu.add(fileMenu);			fileMenu.setMnemonic('F');

		saveMenu.add(saveFields);		saveFields.setMnemonic('e');		saveFields.addActionListener(this);
		saveMenu.add(saveLabels);		saveLabels.setMnemonic('l');		saveLabels.addActionListener(this);
		menu.add(saveMenu);			saveMenu.setMnemonic('S');

		helpMenu.add(helpAbout);		helpAbout.setMnemonic('b');		helpAbout.addActionListener(this);
		menu.add(helpMenu);			helpMenu.setMnemonic('H');		helpMenu.setHorizontalAlignment(RIGHT);

		setJMenuBar(menu);
		
		btn		=new JButton[saveButton+1];
		for (int i=0;i<=saveButton;i++) {
			btn[i]	=new JButton(btnText[i]);
			btn[i].addActionListener(this);
			btn[i].setToolTipText(btnToolTip[i]);
			if (btnIcon[i].compareTo("")!=0) { btn[i].setIcon(new ImageIcon(btnIcon[i])); }
		}
		
		GridBagConstraints	g		=new GridBagConstraints(); g.fill=g.BOTH;

		GridBagLayout		topLayout	=new GridBagLayout();
		JPanel			top		=new JPanel(topLayout);

		GridBagLayout		dataLayout	=new GridBagLayout();
		JPanel			data		=new JPanel(dataLayout);
		
		// Top image (doubles as the quit button)
		g.gridx= 0; g.gridy=0; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0;
		topLayout.setConstraints(btn[quitButton],g); top.add(btn[quitButton]);
		
	// --- ROW 1 ----------------------------------------------------------

		companyText	=new JLabel("Company"); companyText.setHorizontalAlignment(RIGHT);
		g.gridx= 0; g.gridy=0; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(companyText,g); data.add(companyText);
		
		companyName	=new JTextField("",80);
		g.gridx= 1; g.gridy=0; g.gridwidth=3; g.gridheight=1; g.weightx=4.0; g.weighty=0.0; dataLayout.setConstraints(companyName,g); data.add(companyName);
		
		streetText	=new JLabel("Street"); streetText.setHorizontalAlignment(RIGHT);
		g.gridx= 4; g.gridy=0; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(streetText,g); data.add(streetText );

		street		=new JTextField("",30);
		g.gridx= 5; g.gridy=0; g.gridwidth=3; g.gridheight=1; g.weightx=2.0; g.weighty=0.0; dataLayout.setConstraints(street,g); data.add(street);
		
		officePhoneText	=new JLabel("Office"); officePhoneText.setHorizontalAlignment(RIGHT);
		g.gridx= 8; g.gridy=0; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(officePhoneText,g); data.add(officePhoneText);

		JLabel officePhoneText1=new JLabel("Ph#"); officePhoneText1.setHorizontalAlignment(RIGHT);
		g.gridx= 9; g.gridy=0; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(officePhoneText1,g); data.add(officePhoneText1);

		officePhone	=new JTextField("",15);
		g.gridx=10; g.gridy=0; g.gridwidth=1; g.gridheight=1; g.weightx=8.0; g.weighty=0.0; dataLayout.setConstraints(officePhone,g); data.add(officePhone);
		
		btn[searchButton].setMargin(new Insets(1,1,1,1));
		g.gridx=11; g.gridy=0; g.gridwidth=2; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(btn[searchButton],g); data.add(btn[searchButton]);
		
	// --- ROW 2 ----------------------------------------------------------

		lastNameText	=new JLabel("Last Name"); lastNameText.setHorizontalAlignment(RIGHT);
		g.gridx= 0; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(lastNameText,g); data.add(lastNameText);
		
		lastName	=new JTextField("",15);
		g.gridx= 1; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=8.0; g.weighty=0.0; dataLayout.setConstraints(lastName,g); data.add(lastName);
		
		firstNameText	=new JLabel("First Name"); firstNameText.setHorizontalAlignment(RIGHT);
		g.gridx= 2; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(firstNameText,g); data.add(firstNameText);
		
		firstName	=new JTextField("",15);
		g.gridx= 3; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=8.0; g.weighty=0.0; dataLayout.setConstraints(firstName,g); data.add(firstName);
		
		cityText	=new JLabel("City"); cityText.setHorizontalAlignment(RIGHT);
		g.gridx= 4; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(cityText,g); data.add(cityText);

		city		=new JTextField("",20);
		g.gridx= 5; g.gridy=1; g.gridwidth=3; g.gridheight=1; g.weightx=2.0; g.weighty=0.0; dataLayout.setConstraints(city,g); data.add(city);
		
		homePhoneText	=new JLabel("Home"); homePhoneText.setHorizontalAlignment(RIGHT);
		g.gridx= 8; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(homePhoneText,g); data.add(homePhoneText);

		JLabel homePhoneText1	=new JLabel("Ph#"); homePhoneText1.setHorizontalAlignment(RIGHT);
		g.gridx= 9; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(homePhoneText1,g); data.add(homePhoneText1);

		homePhone	=new JTextField("",15);
		g.gridx=10; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=8.0; g.weighty=0.0; dataLayout.setConstraints(homePhone,g); data.add(homePhone);
		
		filter		=new JTextField("",15);
		g.gridx=11; g.gridy=1; g.gridwidth=2; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(filter,g); data.add(filter);

	// --- ROW 3-----------------------------------------------------------
	
		memoText	=new JLabel("Memo"); memoText.setHorizontalAlignment(RIGHT);
		g.gridx= 0; g.gridy=2; g.gridwidth= 1; g.gridheight=2; g.weightx=1.0; g.weighty=1.0; dataLayout.setConstraints(memoText,g); data.add(memoText);
		
		memo		=new JTextArea(3,25);
		JScrollPane msp	=new JScrollPane(memo,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		g.gridx= 1; g.gridy=2; g.gridwidth=3; g.gridheight=2; g.weightx=4.0; g.weighty=1.0; dataLayout.setConstraints(msp,g); data.add(msp);
		
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
		stateText	=new JLabel("State"); stateText.setHorizontalAlignment(RIGHT);
		g.gridx= 4; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(stateText,g); data.add(stateText);
		
		state		=new JTextField("",5);
		g.gridx= 5; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=2.0; g.weighty=0.0; dataLayout.setConstraints(state,g); data.add(state);
		
		zipText		=new JLabel("Zip"); zipText.setHorizontalAlignment(RIGHT);
		g.gridx= 6; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(zipText,g); data.add(zipText);
		
		zip		=new JTextField("",10);
		g.gridx= 7; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=4.0; g.weighty=0.0; dataLayout.setConstraints(zip,g); data.add(zip);

		pagerText	=new JLabel("Pager"); pagerText.setHorizontalAlignment(RIGHT);
		g.gridx= 8; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(pagerText,g); data.add(pagerText);
		
		JLabel pagerText1	=new JLabel("No."); pagerText1.setHorizontalAlignment(RIGHT);
		g.gridx= 9; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(pagerText1,g); data.add(pagerText1);
		
		pager		=new JTextField("",15);
		g.gridx=10; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=8.0; g.weighty=0.0; dataLayout.setConstraints(pager,g); data.add(pager);
		
		btn[addButton].setMargin(new Insets(1,1,1,1));
		g.gridx=11; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(btn[addButton],g); data.add(btn[addButton]);
		
		btn[deleteButton].setMargin(new Insets(1,1,1,1));
		g.gridx=12; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(btn[deleteButton],g); data.add(btn[deleteButton]);
		
	// --- ROW 4 ----------------------------------------------------------

		eMailText	=new JLabel("EMail"); eMailText.setHorizontalAlignment(RIGHT);
		g.gridx= 4; g.gridy=3; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(eMailText,g); data.add(eMailText);
		
		eMail		=new JTextField("",40);
		g.gridx= 5; g.gridy=3; g.gridwidth=4; g.gridheight=1; g.weightx=10.0; g.weighty=0.0; dataLayout.setConstraints(eMail,g); data.add(eMail);
		
		faxText		=new JLabel("Fax"); faxText.setHorizontalAlignment(RIGHT);
		g.gridx= 9; g.gridy=3; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(faxText,g); data.add(faxText);
		
		fax		=new JTextField("",15);
		g.gridx=10; g.gridy=3; g.gridwidth=1; g.gridheight=1; g.weightx=8.0; g.weighty=0.0; dataLayout.setConstraints(fax,g); data.add(fax);
		
		btn[saveButton].setMargin(new Insets(1,1,1,1));
		g.gridx=11; g.gridy=3; g.gridwidth=2; g.gridheight=1; g.weightx=1.0; g.weighty=0.0; dataLayout.setConstraints(btn[saveButton],g); data.add(btn[saveButton]);

	// --------------------------------------------------------------------

		// Data entry area is second item in gridbag
		g.gridx= 0; g.gridy=1; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0;
		topLayout.setConstraints(data,g); top.add(data);

		mainWindow.add(top,BorderLayout.NORTH);
		
		statusMsg	=new JLabel("Loading Data ... Please Wait");
		statusMsg.setHorizontalAlignment(CENTER);
		mainWindow.add(statusMsg,BorderLayout.CENTER);
		
		scrWidth	=(int)(getToolkit().getScreenSize().getWidth());
		scrHeight	=(int)(getToolkit().getScreenSize().getHeight());
		winWidth	=(int)(700);
		winHeight	=(int)(500);
		setBounds((scrWidth-winWidth)/2,(scrHeight-winHeight)/2,winWidth,winHeight);
		disableEditing();
		setVisible(true);
		
		tableModel	=new HW6TableModel();
   		table		=new JTable(tableModel);
		JScrollPane	tablePane	=new JScrollPane(table);
		status("");
		mainWindow.remove(statusMsg);
		statusMsg.setForeground(Color.red);
		g.gridx= 0; g.gridy=2; g.gridwidth=1; g.gridheight=1; g.weightx=1.0; g.weighty=0.0;
		topLayout.setConstraints(statusMsg,g); top.add(statusMsg);
		mainWindow.add(tablePane,BorderLayout.CENTER);
		reDisplayMainWindow();
		table.addMouseListener(this);
		table.getTableHeader().addMouseListener(this);
		table.setSelectionMode(0); // 0=SINGLE_SELECTION (couldn't find it in the API ... was a guess ... it works!)
		showNumberOfRecords();
		goodToGo=true;
	}
		
// --- Homework6:main() -------------------------------------------------------

	public static void main(String args[]) {
		Homework6 HW6=new Homework6();
		HW6.addWindowListener(HW6);
		HW6.addFocusListener(HW6);
	}
	
	public void pause(int time) {
		if (time>0) { try { Thread.sleep(time); } catch(InterruptedException e) { } }
	}
	
// --- Homework6:Window Event Handlers ----------------------------------------
	
	public void windowActivated  (WindowEvent e) 	{ }
	public void windowClosed     (WindowEvent e) 	{ }
	public void windowClosing    (WindowEvent e) 	{ doQuit(); }
	public void windowDeactivated(WindowEvent e) 	{ }
	public void windowDeiconified(WindowEvent e) 	{ }
	public void windowIconified  (WindowEvent e) 	{ }
	public void windowOpened     (WindowEvent e) 	{ }

// --- Homework6:Focus Event Handlers -----------------------------------------

	public void focusGained(FocusEvent e)		{ }
	public void focusLost  (FocusEvent e)		{ }

// --- Homework6:Mouse Event Handler ------------------------------------------

	public void mouseClicked (MouseEvent e)		{
		Object event=e.getSource();
		if (event==table                 ) { doSelectedSomething(); } else
		if (event==table.getTableHeader()) {
			TableColumnModel tcMod	=table.getColumnModel();
			int colIndex		=tcMod.getColumnIndexAtX(e.getX());
			int modelIndex		=tcMod.getColumn(colIndex).getModelIndex();
			tableModel.doSort(modelIndex,this);
		}
	}
 	public void mouseEntered (MouseEvent e)		{ }
	public void mouseExited  (MouseEvent e)		{ }
	public void mousePressed (MouseEvent e)		{ }
	public void mouseReleased(MouseEvent e)		{ }

// --- Homework6:Action Event Handler -----------------------------------------

	public void actionPerformed(ActionEvent e) {
		Object event=e.getSource();
		if (event==fileOpen				) { doFileOpen();	} else
		if (event==fileExit || event==btn[quitButton]	) { doQuit();		} else
		if (event==helpAbout				) { doHelpAbout();	} else
		if (goodToGo) {
			if (event==btn[searchButton]	) { doFilterChanged();		} else
			if (event==btn[addButton   ]	) { doAdd();			} else
			if (event==btn[deleteButton]	) { doDelete();			} else
			if (event==btn[saveButton  ]	) { doSaveData();		} else
			if (event==saveFields		) { doSaveFields();		} else
			if (event==saveLabels		) { doSaveLabels();		} else
			if (event==fieldSelectAll	) { doSaveFieldsSelectAll();	} else
			if (event==fieldSelectNone	) { doSaveFieldsSelectNone();	} else
			if (event==fieldOK		) { doSaveFieldsOK();		} else
			if (event==fieldCancel		) { doSaveFieldsCancel();	}
		} else { errorBox("Please wait until the data has loaded","User Got Too Excited Error"); }
	}
	
// --- Homework6:do...() - Perform a program function -------------------------
	
	public void doQuit() { System.exit(0); }

	public void doNotImplemented(String item) { errorBox(item+" is not implemented","Not Implemented Error"); }
	
	public void doSelectedSomething() {
		tableModel.getRowData(table.getSelectedRow(),this);
		allowEditing();
		origLName=lastName.getText();
		origFName=firstName.getText();
		showNumberOfRecords();
	}
	
	public void doFilterChanged() {
		String newFilter=filter.getText();
		if (newFilter==null) { newFilter=""; }
		tableModel.reQuery(newFilter,this);
	}
	
	public void doAdd() {
		status("Enter data into area above, click Save Data to save the new entry");
		clearSelection();
		recordStatus=INSERT_RECORD;
		allowEditing();
	}
	
	public void doDelete() {
		switch (recordStatus) {
			case NO_RECORD:		clearSelection(); errorBox("There is no record to delete","Delete Error"); break;
			case INSERT_RECORD:	clearSelection(); showNumberOfRecords(); break;
			case UPDATE_RECORD:	tableModel.deleteRow(this); break;
		}
	}
	
	public void doSaveData() {
		switch (recordStatus) {
			case NO_RECORD:		clearSelection(); errorBox("There is no record to save","Save Error"); break;
			case INSERT_RECORD:	tableModel.addRow(this); break;
			case UPDATE_RECORD:	tableModel.updateRow(this); break;
		}
	}
	
	public void doFileOpen() {
		errorBox(
			"Hmmm....\n"+
			"--------------------------------------------------------------------------------------------------\n"+
			"This option doesn't make sense.  I don't know how to implement it because\n"+
			"I don't know what the function is supposed to do!\n\n"+
			"The data displayed in the text area on the screen is either:\n\n"+
			"(A) The result of a new record add operation\n"+
			"(B) The result of retrieving a record from the SQL result set\n\n"+
			"Data in a saved field text file has many records, and they all exist\n"+
			"in the database, so WHICH ONE of them should be displayed in the text\n"+
			"area?  Do I take just the first one, guess at one, take the last one, or\n"+
			"what?  Then, what would be the status of that data?  Is it a new record\n"+
			"waiting to be added?  Is it an existing record waiting to be updated?  Is\n"+
			"it read-only?\n\n"+
			"I would implement this function, but ...... I've done enough.  Time to move\n"+
			"on to the next (final) project.",
			"Doesn't Make Sense Error"); 
	}
	
	private boolean		fieldSelect[],fieldCancelled;
	private JDialog		fieldJD;
	private JCheckBox	fieldCB[];
	private JButton		fieldSelectAll,fieldSelectNone,fieldOK,fieldCancel;
	private final int	LABELS=0,FIELDS=1;
	private int		fieldCol;
	public void doSaveFields() {
		fieldCol	=tableModel.getDatabaseColumnCount();
		fieldCancelled	=false;
		fieldJD		=new JDialog(this,"Select Fields for Export",true);
		fieldJD.setDefaultCloseOperation(fieldJD.DISPOSE_ON_CLOSE);
		Container jdc	=fieldJD.getContentPane();
		JPanel	jPanE	=new JPanel(new GridLayout(fieldCol,1));
		fieldSelect	=new boolean[fieldCol]; for (int i=0;i<fieldCol;i++) { fieldSelect[i]=true; }
		fieldCB		=new JCheckBox[fieldCol];
		for (int i=0;i<fieldCol;i++) {
			fieldCB[i]=new JCheckBox(tableModel.getDatabaseColumnName(i),fieldSelect[i]);
			jPanE.add(fieldCB[i]);
		}
		jdc.add(jPanE,BorderLayout.EAST);
		JPanel	jPanW	=new JPanel(new GridLayout(4,1));
		fieldSelectAll	=new JButton("Select All");	fieldSelectAll .addActionListener(this); jPanW.add(fieldSelectAll);
		fieldSelectNone	=new JButton("Select None");	fieldSelectNone.addActionListener(this); jPanW.add(fieldSelectNone);
		fieldOK		=new JButton("OK");		fieldOK        .addActionListener(this); jPanW.add(fieldOK);
		fieldCancel	=new JButton("Cancel");		fieldCancel    .addActionListener(this); jPanW.add(fieldCancel);
		jdc.add(jPanW,BorderLayout.WEST);
		int winWidth	=(int)(300);
		int winHeight	=(int)(350);
		fieldJD.setBounds((scrWidth-winWidth)/2,(scrHeight-winHeight)/2,winWidth,winHeight);
		fieldJD.setVisible(true);
		while (fieldJD.isShowing()) { pause(250); }
		if (!fieldCancelled) {
			for (int i=0;i<fieldCol;i++) { fieldSelect[i]=fieldCB[i].isSelected(); }
			doSaveSomething(FIELDS,"Select Field Data File",new ExtensionFilter("Field Data files (*.fdf)","fdf"));
		}
	}
	public void doSaveFieldsSelectAll()  { doSaveFieldsSelect(true ); }
	public void doSaveFieldsSelectNone() { doSaveFieldsSelect(false); }
	public void doSaveFieldsSelect(boolean selectState) {
		for (int i=0;i<fieldCol;i++) {
			fieldSelect[i]=selectState;
			fieldCB[i].setSelected(selectState);
		}
	}
	public void doSaveFieldsOK()     { fieldJD.dispose(); }
	public void doSaveFieldsCancel() { fieldCancelled=true; fieldJD.dispose(); }
	public void doSaveLabels() {
		doSaveSomething(LABELS,"Select Address Label File",new ExtensionFilter("Label files (*.lbl)","lbl"));
	}
	private void doSaveSomething(int saveWhat,String selectTitle,ExtensionFilter ef) {
		JFileChooser jfc=new JFileChooser(".");
		jfc.setDialogTitle(selectTitle);
		jfc.setFileFilter(ef);
		jfc.setFileSelectionMode(jfc.FILES_AND_DIRECTORIES);
		if (jfc.showSaveDialog(new JPanel())==jfc.APPROVE_OPTION) {
			String fileName=jfc.getName(jfc.getSelectedFile());
			String pathName=jfc.getCurrentDirectory().toString();
			if (fileName!=null && fileName.compareTo("null")!=0) {
				if (saveWhat==LABELS) {  tableModel.doSaveLabels(pathName+"\\"+fileName,            this); } else
				if (saveWhat==FIELDS) {  tableModel.doSaveFields(pathName+"\\"+fileName,fieldSelect,this); }
			}
		}
	}
	
	public void doHelpAbout() {
		infoBox("Paul Schoberg\n"+
			"CSUB CS294, Spring 2000\n"+
			"------------------------------------------------------------------------\n"+
			"Dr. Wang\n\n"+
			"JDBC Database Connectivity\n\n"+
			"This program connects to the CSUB Cheval address book database\n"+
			"and manipulates the data contained therein.",
			"Help About..." );
	}
	
// --- Homework6: Utility Functions -------------------------------------------
	
	public void reDisplayMainWindow() {
		mainWindow.doLayout();
		mainWindow.setVisible(false);
		mainWindow.setVisible(true);
	}
	
	public void clearSelection() {
		companyName.setText("");
		lastName   .setText("");
		firstName  .setText("");
		officePhone.setText("");
		homePhone  .setText("");
		pager      .setText("");
		fax        .setText("");
		street     .setText("");
		city       .setText("");
		state      .setText("");
		zip        .setText("");
		eMail      .setText("");
		memo       .setText("");
		table.removeRowSelectionInterval(0,tableModel.getRowCount());
		recordStatus=NO_RECORD;
		origLName=""; origFName="";
		disableEditing();
	}
	
	public void allowEditing()   { setEditingState(true);  }
	public void disableEditing() { setEditingState(false); }
	private void setEditingState(boolean x) {
		Color	bgColour;
		if (x) { bgColour=Color.white; } else { bgColour=Color.lightGray; }
		companyName.setEnabled(x); companyName.setEditable(x); companyName.setBackground(bgColour);
		lastName   .setEnabled(x); lastName   .setEditable(x); lastName   .setBackground(bgColour);
		firstName  .setEnabled(x); firstName  .setEditable(x); firstName  .setBackground(bgColour);
		officePhone.setEnabled(x); officePhone.setEditable(x); officePhone.setBackground(bgColour);
		homePhone  .setEnabled(x); homePhone  .setEditable(x); homePhone  .setBackground(bgColour);
		pager      .setEnabled(x); pager      .setEditable(x); pager      .setBackground(bgColour);
		fax        .setEnabled(x); fax        .setEditable(x); fax        .setBackground(bgColour);
		street     .setEnabled(x); street     .setEditable(x); street     .setBackground(bgColour);
		city       .setEnabled(x); city       .setEditable(x); city       .setBackground(bgColour);
		state      .setEnabled(x); state      .setEditable(x); state      .setBackground(bgColour);
		zip        .setEnabled(x); zip        .setEditable(x); zip        .setBackground(bgColour);
		eMail      .setEnabled(x); eMail      .setEditable(x); eMail      .setBackground(bgColour);
		memo       .setEnabled(x); memo       .setEditable(x); memo       .setBackground(bgColour);
	}
	
	public void errorBox (String msg,String title            ) { dialogBox(msg,title,JOptionPane.ERROR_MESSAGE       ); }
	public void infoBox  (String msg,String title            ) { dialogBox(msg,title,JOptionPane.INFORMATION_MESSAGE ); }
	public void dialogBox(String msg,String title,int msgType) { JOptionPane.showMessageDialog(null,msg,title,msgType); }
	
	public void status(String msg)    { statusMsg.setText(msg); }
	public void showNumberOfRecords() { status("Number of records: "+tableModel.getRowCount()); }
}